﻿#include "CardViewModel.h"

using namespace Views::Models::Network;

CardViewModel::CardViewModel(Cards::CardsCollection cards)
	: cards(cards), card(NULL)
{
}

CardViewModel::CardViewModel(Cards::Card* card)
	: card(card)
{
}

sf::Packet& Views::Models::Network::operator <<(sf::Packet& Packet, const CardViewModel& model)
{
	if (dynamic_cast<Cards::PlayerCard*>(model.card) != NULL)
	{
		Packet << sf::Uint8(0);
		Packet << sf::Uint8(dynamic_cast<Cards::PlayerCard*>(model.card)->getPiece()->getColour());
	}
	else if (dynamic_cast<Cards::WeaponCard*>(model.card) != NULL)
	{
		Packet << sf::Uint8(1);
		Packet << sf::Uint8(dynamic_cast<Cards::WeaponCard*>(model.card)->getPiece()->getType());
	}
	else if (dynamic_cast<Cards::RoomCard*>(model.card) != NULL)
	{
		Packet << sf::Uint8(2);
		Packet << sf::Uint8(dynamic_cast<Cards::RoomCard*>(model.card)->room->type);
	}
	return Packet;
}

sf::Packet& Views::Models::Network::operator >>(sf::Packet& Packet, CardViewModel& model)
{
	sf::Uint8 type, tmp;
	Packet >> type >> tmp;
	switch(type)
	{
	case 0:
		{
			Cards::PlayersCards cards = model.cards.getPlayersCards();
			for(Cards::PlayersCards::iterator i = cards.begin(); i != cards.end(); ++i)
			{
				if ((*i)->getPiece()->getColour() == tmp)
					model.card = *i;
			}
		}
		break;
	case 1:
		{
			Cards::WeaponsCards cards = model.cards.getWeaponsCards();
			for(Cards::WeaponsCards::iterator i = cards.begin(); i != cards.end(); ++i)
			{
				if ((*i)->getPiece()->getType() == tmp)
					model.card = *i;
			}
		}
		break;
	case 2:
		{
			Cards::RoomsCards cards = model.cards.getRoomsCards();
			for(Cards::RoomsCards::iterator i = cards.begin(); i != cards.end(); ++i)
			{
				if ((*i)->room->type == tmp)
					model.card = *i;
			}
		}
		break;
	}
	return Packet;
}
